﻿using System;
using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Expando;
using System.Net;
using System.Drawing;
using System.IO;

using SHDocVw;

using Microsoft.Win32;
namespace aslive.pasterImageToWebUpload.ie.BHO
{
    [ComVisible(true),
    ClassInterface(ClassInterfaceType.None),
    Guid("DA8EA345-02AE-434E-82E9-448E3DB7629E"),
    ProgId("MyExtension"),
    ComDefaultInterface(typeof(IExtension))]

    public class BHO:IObjectWithSite,IExtension
    {
        SHDocVw.WebBrowser webBroswer;

        public void onDocumentComplete(object pDisp,ref object URL){

            dynamic window = webBroswer.Document.parentWindow;
            IExpando windowEx = (IExpando)window;
            windowEx.AddProperty("myExtension");
            window.myExtension = this;


        }
        int IObjectWithSite.SetSite(object site)
        {
            if (site != null)
            {
                webBroswer = (SHDocVw.WebBrowser)site;
                webBroswer.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.onDocumentComplete);
            }
            else {
                webBroswer.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.onDocumentComplete);    
            }
            return 0;
        }

        int IObjectWithSite.GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(webBroswer);
            int hr = Marshal.QueryInterface(punk,ref guid,out ppvSite);
            Marshal.Release(punk);
            return hr;
        }


        public Boolean paster(string postUrl)
        {
            MessageBox.Show("开始");
            var extName = "";
            if (Clipboard.ContainsImage()) {
                var xx = Clipboard.GetDataObject();
                MessageBox.Show(xx.GetFormats().ToString());
                Image img = Clipboard.GetImage();
                MessageBox.Show("pixelFormat:"+img.PixelFormat+"   "+"rawFormat:"+img.RawFormat);
                img.Save("__________tem");

                FileStream fs = new FileStream("__________tem", FileMode.Open, FileAccess.Read);
                System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                string fileType = string.Empty; 

                try
                {
                    byte data = br.ReadByte();
                    fileType += data.ToString();
                    data = br.ReadByte();
                    fileType += data.ToString();
                    FileExtension extension;

                    try
                    {
                        extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
                    }
                    catch 
                    {

                        return false;
                    }
                    extName = extension.ToString();
                    }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                        br.Close();
                    }
                }
                MessageBox.Show(extName);
                var fname="";
                switch (extName) { 
                    case "JPG":
                        fname="windbell2_temPasterImg.jpg";
                        img.Save(fname);
                        break;
                    case "BMP":
                        fname="windbell2_temPasterImg.jpg";
                        img.Save(fname,System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                    case "PNG":
                        fname = "windbell2_temPasterImg.png";
                        img.Save(fname + extName, System.Drawing.Imaging.ImageFormat.Png);
                        break;
                    case "GIF":
                        fname = "windbell2_temPasterImg.gif";
                        img.Save(fname, System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                    default:
                        return false;
                }
                MessageBox.Show(fname);
                WebClient wc = new WebClient();
                wc.UploadFile(postUrl, "POST", fname);
                return true;               
            }
            return false;
        }
        public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
        [ComRegisterFunction]
        public static void RegisterBHO(Type type) {
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
            if (rk == null) {
                rk = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);
            }
            string guid = type.GUID.ToString("B");
            RegistryKey ok = rk.OpenSubKey(guid);

            if (ok == null)
            {
                ok = rk.CreateSubKey(guid);
            }
            ok.SetValue("Alright",1);
            rk.Close();
            ok.Close();


        }
    }
public enum FileExtension
    {
        JPG = 255216,
        GIF = 7173,
        PNG = 13780,
        //SWF = 6787,
        //RAR = 8297,
        //ZIP = 8075,
        //_7Z = 55122,
        //VALIDFILE=9999999,
        // 255216 jpg;  

        // 7173 gif;  

        BMP=6677,  

        // 13780 png;  

        // 6787 swf  

        // 7790 exe dll,  

        // 8297 rar  

        // 8075 zip  

        // 55122 7z  

        // 6063 xml  

        // 6033 html  

        // 239187 aspx  

        // 117115 cs  

        // 119105 js  

        // 102100 txt  

        // 255254 sql   

    } 
}
